import GameConfig from "./GameConfig";
import { MyAnimationCache } from "./script/shader/MyAnimationCache";
import { MyShaderAnimation } from "./script/shader/MyShaderAnimation";
class Main {
    constructor() {
        //根据IDE设置初始化引擎		
        if (window["Laya3D"]) Laya3D.init(GameConfig.width, GameConfig.height);
        else Laya.init(GameConfig.width, GameConfig.height, Laya["WebGL"]);
        Laya["Physics"] && Laya["Physics"].enable();
        Laya["DebugPanel"] && Laya["DebugPanel"].enable();
        Laya.stage.scaleMode = GameConfig.scaleMode;
        Laya.stage.screenMode = GameConfig.screenMode;
        Laya.stage.alignV = GameConfig.alignV;
        Laya.stage.alignH = GameConfig.alignH;
        Laya.Stat.show();
        //兼容微信不支持加载scene后缀场景
        Laya.URL.exportSceneToJson = GameConfig.exportSceneToJson;

        //打开调试面板（通过IDE设置调试模式，或者url地址增加debug=true参数，均可打开调试面板）
        if (GameConfig.debug || Laya.Utils.getQueryString("debug") == "true") Laya.enableDebugPanel();
        if (GameConfig.physicsDebug && Laya["PhysicsDebugDraw"]) Laya["PhysicsDebugDraw"].enable();
        if (GameConfig.stat) Laya.Stat.show();
        Laya.alertGlobalError(true);

        //激活资源版本控制，version.json由IDE发布功能自动生成，如果没有也不影响后续流程
        Laya.ResourceVersion.enable("version.json", Laya.Handler.create(this, this.onVersionLoaded), Laya.ResourceVersion.FILENAME_VERSION);
    }

    onVersionLoaded(): void {
        //激活大小图映射，加载小图的时候，如果发现小图在大图合集里面，则优先加载大图合集，而不是小图
        Laya.AtlasInfoManager.enable("fileconfig.json", Laya.Handler.create(this, this.onConfigLoaded));
    }

    onConfigLoaded(): void {
        console.log('.......');
        Laya.loader.load("res/atlas/plants.atlas", Laya.Handler.create(this, this.testAtlas), null, Laya.Loader.ATLAS);
    }

    private testAtlas() {
        console.log('testAtlas');
        // 解析图集文件
        let atlas = Laya.loader.getRes("res/atlas/plants.atlas");
        let frames = atlas.frames;
        let anis = {};
        for (let k in frames) {
            let pat = /_[0-9]+.png/;
            if (pat.test(k)) {
                let endstr = k.match(/_[0-9]+.png/)[0];
                let aniName = k.replace(endstr, "");
                if (!anis[aniName]) {
                    anis[aniName] = [];
                }
                let frame = Laya.loader.getRes("plants/" + k);
                anis[aniName].push(
                    {
                        frame: frame,
                        num: parseInt(endstr.replace(".png", "").replace("_", ""))
                    });
            } else {
                let aniName = k.replace(".png", "");
                anis[aniName] = [{
                    frame: Laya.loader.getRes("plants/" + k),
                    num: 0
                }];
            }
        }

        let aniNames = [];
        for (let aniName in anis) {
            anis[aniName].sort((a, b) => {
                return a.num = b.num;
            });
            let fs = anis[aniName].map((item) => {
                return item.frame;
            });
            MyAnimationCache.addFrames(aniName, fs);
            aniNames.push(aniName);
        }
        for (let i = 0; i < 1000; i++) {
            let ani = new MyShaderAnimation();
            ani.scaleX = 0.25;
            ani.scaleY = 0.25;
            ani.x = Math.random() * 400;
            ani.y = Math.random() * 400;
            let aniName = aniNames[Math.floor(aniNames.length * Math.random())];
            console.log('name', aniName);
            ani.playAni(aniName);
            ani.interval = 50;
            Laya.stage.addChild(ani);
        }
    }
}
//激活启动类
new Main();
